package com.fcgzs.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fcgzs.springboot.mapper.MemberMapper;
import com.fcgzs.springboot.mapper.OrderMapper;
import com.fcgzs.springboot.mapper.OrdersettingMapper;
import com.fcgzs.springboot.mapper.SetmealMapper;
import com.fcgzs.springboot.pojo.Member;
import com.fcgzs.springboot.pojo.Order;
import com.fcgzs.springboot.pojo.Ordersetting;
import com.fcgzs.springboot.pojo.Setmeal;
import com.fcgzs.springboot.service.OrderService;
import com.fcgzs.springboot.util.DateUtils;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Component
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrdersettingMapper ordersettingMapper;
    @Resource
    private MemberMapper memberMapper; //会员
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private SetmealMapper setmealMapper;


    //提交预约
    @Override
    public Result submitOrder(Map map) {
        /* 分析：
        * 验证选中日期是否可以预约
        *   1.不可以，返回提示信息
        *   2.可以
        *       a:预约人满，返回提示信息
        *       b:不是会员，填写会员信息完成预约
        *       c:是会员，已经预约不能重复
        * */
        try {
            String datestr = (String) map.get("orderDate");

            QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("orderDate", DateUtils.parseString2Date(datestr));
            Ordersetting ordersetting = ordersettingMapper.selectOne(queryWrapper);
            if(ordersetting!=null){
                if(ordersetting.getNumber()<=ordersetting.getReservations()){
                    return new Result(false,"该日期已约满");
                }
                //同一用户、同一天、同一套餐
                QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
                memberQueryWrapper.eq("idCard",(String)map.get("idCard"));
                Member member = memberMapper.selectOne(memberQueryWrapper);
                //存在该会员
                if(member!=null){
                    QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
                    orderQueryWrapper.eq("member_id",member.getId());
                    orderQueryWrapper.eq("orderDate",DateUtils.parseString2Date(datestr));
                    orderQueryWrapper.eq("setmeal_id",Integer .parseInt((String)map.get("setmealId")));
                    Order order = orderMapper.selectOne(orderQueryWrapper);
                    if(order!=null){
                        return new Result(false,"不能重复预约");
                    }
                }else{
                    //不存在该会员
                    member = new Member();
                    member.setIdcard((String)map.get("idCard"));
                    member.setSex((String)map.get("sex"));
                    member.setName((String)map.get("name"));
                    member.setPhonenumber((String)map.get("telephone"));
                    member.setRegtime(LocalDate.now());
                    memberMapper.insert(member);
                }

                //预约成功后
                Order order = new Order();
                order.setMemberId(member.getId());
                order.setSetmealId(Integer .parseInt((String)map.get("setmealId")));
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate date = LocalDate.parse(datestr, dateTimeFormatter);
                order.setOrderdate(date);
                order.setOrdertype("微信预约");
                orderMapper.insert(order);

                //修改预约表
                ordersetting.setReservations(ordersetting.getReservations()+1);
                ordersettingMapper.updateById(ordersetting);

                return new Result(true,"预约成功",order.getId());
            }else{
                return new Result(false,"该日期未开放预约");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    //预约成功、展示套餐信息
    @Override
    public Result findOrderById(Integer id) {
        Map<String,Object> map = new HashMap<>();
        Order order = orderMapper.selectById(id);
        map.put("orderType",order.getOrdertype()); //预约类型
        LocalDate orderdate = order.getOrderdate();
        map.put("orderDate",orderdate.getYear()+"-"+(orderdate.getMonthValue()+1)+"-"+orderdate.getDayOfMonth()); //体检日期
        Setmeal setmeal = setmealMapper.selectById(order.getSetmealId());
        map.put("setmeal",setmeal.getName()); //体检套餐
        Member member = memberMapper.selectById(order.getMemberId());
        map.put("member",member.getName()); //体检人
        return new Result(true,"",map);
    }
}
